package com.ctsi.offlinesupport.service;

import com.ctsi.offlinesupport.constant.FlowStatusConstant;
import com.ctsi.offlinesupport.constant.SupportFlowConstant;
import com.ctsi.offlinesupport.dto.SupportResultDTO;
import com.ctsi.supportflow.app.entity.SupportInfo;
import com.ctsi.supportflow.app.entity.SupportTask;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

@Component("autoEndDelegate")
public class AutoEndDelegate implements JavaDelegate, Serializable {
    private static final Logger logger = LoggerFactory.getLogger(AutoEndDelegate.class);
    private static final long serialVersionUID = 2566648068323823333L;

    @Autowired
    private OfflineSupportService offlineSupportService;

    @Autowired
    private SupportInfoService supportInfoService;

    @Autowired
    private SupportTaskService supportTaskService;

    @Override
    public void execute(DelegateExecution execution) {
        logger.warn("自动办结开始");
        String businessKey = (String) execution.getVariable(SupportFlowConstant.Variables.BUSINESSKEY.name);
        SupportInfo info = new SupportInfo();
        info.setId(businessKey);
        //设置完成状态
        info.setCurFlowStatus(FlowStatusConstant.COMPLETE.value);
        info.setFlowOverTime(System.currentTimeMillis());
        supportInfoService.updateSupportInfoByPKSelective(info);
        //评价信息
        List<SupportResultDTO> dtos = supportTaskService.findAllSupportTaskDTO4AutoEnd(businessKey);
        dtos.forEach(dto -> {
            String _taskId = dto.getSupportTaskId();
            SupportTask supportTask = new SupportTask();
            supportTask.setTaskId(_taskId);
            supportTask.setWorkLoad(new BigDecimal(dto.getWorkLoad()));
            supportTask.setEvalContent("系统自动办结，默认好评!");
            supportTask.setEvalScore(10);
            this.supportTaskService.updateSupportTaskByPKSelective(supportTask);
        });
        logger.warn("自动办结结束");
    }


}
